﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace nMongo
{
    internal class FieldOrPropertyInfo
    {
        internal FieldInfo Field { get; set; }
        internal PropertyInfo Property { get; set; }
        internal Type MemberType
        {
            get
            {
                return this.IsField ? Field.FieldType : (this.IsProperty ? Property.PropertyType : null);
            }
        }

        internal bool IsField
        {
            get { return Field != null; }
        }

        internal bool IsProperty
        {
            get { return Property != null; }
        }
    }

    internal static class TypeExtensions
    {
        internal static FieldOrPropertyInfo GetFieldOrProperty(this Type type, string name)
        {
            FieldOrPropertyInfo @return = new FieldOrPropertyInfo();
            @return.Field = type.GetField(name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            if (!@return.IsField)
            {
                @return.Property = type.GetProperty(name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            }
            return @return;
        }
    }
}
